package com.zhtools.utils;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * add by zhanghao at 2017/11/26
 */
public class DateUtils {
    private static final String PATTERN_MONTH_DAY = "yyyy-MM-dd";
    private static final String PATTERN_STRING_TO_DATE = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前月
     *
     * @return
     */
    public static Integer getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        return month;
    }

    /**
     * 获取当前年
     *
     * @return
     */
    public static Integer getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return year;
    }

    /**
     * 获取昨天日期
     * 格式:yyyy-MM-dd
     *
     * @return
     */
    public static String getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_MONTH_DAY);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取当前时间的前beforeDays天的日期
     * 格式:yyyy-MM-dd
     *
     * @param beforeDays
     * @return
     */
    public static String getBeforeDays(Integer beforeDays) {
        beforeDays = Math.abs(beforeDays);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 0 - beforeDays);
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_MONTH_DAY);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 月份的第一天日期
     * 格式:yyyy-MM-dd
     *
     * @param month
     * @return
     */
    public static String monthFristDay(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_MONTH_DAY);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 月份的最后一天日期
     * 格式:yyyy-MM-dd
     *
     * @param month
     * @return
     */
    public static String monthLastDay(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 0);

        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_MONTH_DAY);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取指定月的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static Integer[] monthDays(Integer year, Integer month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);

        List<Integer> days = new ArrayList<>();
        for (int day = 1; day <= maxDate; day++) {
            days.add(day);
        }

        return days.toArray(new Integer[]{});
    }

    /**
     * 字符串转Date
     * 格式:yyyy-MM-dd HH:mm:ss
     *
     * @param string
     * @return
     */
    public static Date stringToDate(String string) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_STRING_TO_DATE);
        Date date = null;
        try {
            date = dateFormat.parse(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date stringToDate(String string, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = dateFormat.parse(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * date转字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * LocalDateTime 转字符串
     *
     * @return
     */
    public static String localDateTimeToString(LocalDateTime date, String pattern) {
        pattern = StringUtils.isEmpty(pattern) ? PATTERN_STRING_TO_DATE : pattern;
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 增加或者减少days天
     *
     * @param date
     * @return
     */
    public static Date addDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    /**
     * Date 类型转 LocalDateTime 类型
     *
     * @param date
     * @return
     */
    public static LocalDateTime toLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return LocalDateTime.of(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH),
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND),
                calendar.get(Calendar.MILLISECOND)
        );
    }

    /**
     * String 类型转 LocalDateTime 类型
     *
     * @param date
     * @param pattern 默认格式是 "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static LocalDateTime toLocalDateTime(String date, String pattern) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? PATTERN_STRING_TO_DATE : pattern);
        return LocalDateTime.parse(date, formatter);
    }

//    public static void main(String[] args) {
//        System.out.println("当前年:" + DateUtils.getCurrentYear());
//        System.out.println("当前月:" + DateUtils.getCurrentMonth());
//        System.out.println("当前月第一天:" + DateUtils.monthFristDay(2017, 10));
//        System.out.println("当前月最后一天:" + DateUtils.monthLastDay(2017, 10));
//        System.out.println("昨天日期:" + DateUtils.getYesterday());
//        System.out.println("和12整除:" + 0 / 12);
//        System.out.println("和12整除:" + 6 / 12);
//        System.out.println("和12整除:" + 11 / 12);
//        System.out.println("和12整除:" + 12 / 12);
//        System.out.println("和12整除:" + 15 / 12);
//
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        int houre = calendar.get(Calendar.HOUR_OF_DAY);
//        System.out.println(houre);
//    }

    public enum AmOrPm {
        AM("1", "上午"), PM("2", "下午");

        private String code;
        private String name;

        AmOrPm(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public static AmOrPm get(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int houre = calendar.get(Calendar.HOUR_OF_DAY);
            if (houre / 12 == 0) {
                return AmOrPm.AM;
            }
            if (houre / 12 >= 1) {
                return AmOrPm.PM;
            }
            return null;
        }
    }
}
